package com.i2works.smartluolong.utils.shiro.permission;

import org.apache.shiro.authz.Permission;
import org.apache.shiro.util.AntPathMatcher;
import org.apache.shiro.util.PatternMatcher;

/**
 * 自定义Permission
 *
 * @author koal <koal@vip.qq.com>
 * @date 2016/1/17 15:14
 */

public class UrlPermission implements Permission {

    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public UrlPermission() {
    }

    public UrlPermission(String url) {
        this.url = url;
    }

    @Override
    public boolean implies(Permission p) {

        if (!(p instanceof UrlPermission)){
            return false;
        }else {
            UrlPermission urlPermission =(UrlPermission)p;
            PatternMatcher patternMatcher = new AntPathMatcher();
            //System.err.println("数据库："+this.getUrl()+",当前："+urlPermission.getUrl()+",结果："+patternMatcher.matches(this.getUrl(),urlPermission.getUrl()));
            //自定义（当前访问）urlpermission的url和传递过来(数据库)的permission的url进行对比
            return patternMatcher.matches(this.getUrl(),urlPermission.getUrl());
        }
    }
}
